<?php
/**
 * User: i@fedax.cn
 * Time: 2018/7/26 14:33
 */

namespace app\admin\model;


class Task extends DataBase
{
    protected $name = 'mission';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    public function getStartTimeAttr($value=0)
    {
        return date('Y-m-d H:i:s', $value);
    }
    public function getEndTimeAttr($value=0)
    {
        return date('Y-m-d H:i:s', $value);
    }
    /**
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getDataList()
    {

        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );

        $start_time =strtotime(input('start_time'));
        $end_time = strtotime(input('end_time'));
        $key = input('key');
        $value = input('value');
        $where = [];
        if(!empty($start_time) && !empty($end_time)){
            $where[] = ['expire_time','>=',$start_time];
            $where[] = ['expire_time','<=',$end_time];
        }elseif (!empty($start_time)){
            $where[] = ['expire_time','>=',$start_time];
        }elseif (!empty($end_time)){
            $where[] = ['expire_time','<=',$end_time];
        }


        if(!empty($key) && !empty($value)){
            if($value == 'id'){
                $where[] = [$key,'=',$value];
            }else{
                $where[] = [$key,'like','%'.$value.'%'];
            }

            // halt($map);
        }

        return $this->alias('a')->join('gift_url b','a.gift_id = b.id','left')->field('a.*,b.gift_name')
            ->where($where)
            ->order('id desc')
            ->paginate($page_config);
    }

    public function add()
    {
        $map = ['u_id'=>'u_number','id'=>'user_id','r_id'=>'user_id'];
        //halt($_FILES);
        $newData['m_name'] = input('post.m_name');
        $newData['gift_id'] = input('post.gift_id');
        $newData['num'] = input('post.num');
        $newData['level'] = input('post.level');
        $newData['advance_level'] = input('post.advance_level');
        $newData['empirical_value'] = input('post.empirical_value');
        $newData['m_type'] = input('post.m_type');
        $newData['expire_time'] = strtotime(input('post.expire_time'));
        $type = input('post.type');
//        if(!empty($type)){
//            $userId = User::where('u_number','=',$type)->value('user_id');
//            if(empty($userId)) return ['code'=>-100,'msg'=>'用户不存在'];
//            $newData['roomId'] = Room::where('user_id','=',$userId)->value('id');
//            if(empty($newData['roomId']))  return ['code'=>-100,'msg'=>'房间不存在'];
//            $newData['type'] = 2;
//        }else{
//            $newData['type'] = 0;
//            $newData['roomId'] = null;
//        }
        $res = $this->save($newData);
        return $res;
    }

    public function taskEdit()
    {
        $id = input('get.id');
        $data = $this->where('id','=',$id)->find();
        return $data;
    }

    public function editData()
    {
        $id = input('get.id');
        $newData = input('get.');

        $newData['expire_time'] = strtotime($newData['expire_time']);
//        $newData['end_time'] = strtotime($newData['end_time']);

        $res = $this->save($newData,['id'=>$id]);

        if($res)  return ['code'=>100,'msg'=>'修改成功'];
        return ['code'=>-100,'msg'=>'修改失败'];
    }
}